package com.bruce.pullrefresh;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2015/8/19.
 */
public class PullToRefreshRecyclerView extends PullToRefreshBase<RecyclerView> {
    public PullToRefreshRecyclerView(Context context) {
        super(context);
    }

    public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreshRecyclerView(Context context, Mode mode) {
        super(context, mode);
    }

    public PullToRefreshRecyclerView(Context context, Mode mode, AnimationStyle animStyle) {
        super(context, mode, animStyle);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        return new ExRecyclerView(context,attrs);
    }

    @Override
    protected boolean isReadyForPullEnd() {
        return isLastItemVisible();
    }

    @Override
    protected boolean isReadyForPullStart() {
        return isFirstItemVisible();
    }

    private boolean isFirstItemVisible() {
        final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

        if (null == adapter || adapter.getItemCount()==0) {
          /*  if (DEBUG) {
                Log.d(LOG_TAG, "isFirstItemVisible. Empty View.");
            }*/
            return true;

        } else {

            /**
             * This check should really just be:
             * mRefreshableView.getFirstVisiblePosition() == 0, but PtRListView
             * internally use a HeaderView which messes the positions up. For
             * now we'll just add one to account for it and rely on the inner
             * condition which checks getTop().
             */
            if (findFirstVisiblePosition()<= 1) {
                final View firstVisibleChild = mRefreshableView.getChildAt(0);
                if (firstVisibleChild != null) {
                    return firstVisibleChild.getTop() >= mRefreshableView.getTop();
                }
            }
        }

        return false;
    }

    private boolean isLastItemVisible() {
        final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

        if (null == adapter || adapter.getItemCount()==0) {
            /*if (DEBUG) {
                Log.d(LOG_TAG, "isLastItemVisible. Empty View.");
            }*/
            return true;
        } else {
            final int lastItemPosition = mRefreshableView.getAdapter().getItemCount()-1 ;
          //  final int lastVisiblePosition = mRefreshableView.getLastVisiblePosition();
            final int lastVisiblePosition = findLastVisiblePosition();
            Log.d("mytest","lastItemPosition=="+lastItemPosition+"==lastVisiblePosition="+lastVisiblePosition);

           /* if (DEBUG) {
                Log.d(LOG_TAG, "isLastItemVisible. Last Item Position: " + lastItemPosition + " Last Visible Pos: "
                        + lastVisiblePosition);
            }*/

            /**
             * This check should really just be: lastVisiblePosition ==
             * lastItemPosition, but PtRListView internally uses a FooterView
             * which messes the positions up. For me we'll just subtract one to
             * account for it and rely on the inner condition which checks
             * getBottom().
             */
            if (lastVisiblePosition >= lastItemPosition - 1) {
                final int childIndex = lastVisiblePosition -findFirstVisiblePosition();
                final View lastVisibleChild = mRefreshableView.getChildAt(childIndex);
                if (lastVisibleChild != null) {
                    return lastVisibleChild.getBottom() <= mRefreshableView.getBottom();
                }
            }
        }

        return false;
    }
    public static final int INVALID_POSITION =- Integer.MAX_VALUE;
    public  int findLastVisiblePosition(){



        int[] pos=new int[2];
        RecyclerView.LayoutManager layoutManager = mRefreshableView.getLayoutManager();
        if (layoutManager != null) {
            if (layoutManager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) layoutManager;
                lm.findLastVisibleItemPositions(pos);
            } else if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gm = (GridLayoutManager) layoutManager;
                pos[0] = gm.findLastVisibleItemPosition();
                pos[1] = INVALID_POSITION;
            } else if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager lm = (LinearLayoutManager) layoutManager;
                pos[0] = lm.findLastVisibleItemPosition();
                pos[1] = INVALID_POSITION;
            }
        }

        return Math.max(pos[0],pos[1]);
    }

    public  int findFirstVisiblePosition(){



        int[] pos=new int[2];
        RecyclerView.LayoutManager layoutManager = mRefreshableView.getLayoutManager();
        if (layoutManager != null) {
            if (layoutManager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) layoutManager;
                lm.findFirstVisibleItemPositions(pos);
            } else if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gm = (GridLayoutManager) layoutManager;
                pos[0] = gm.findFirstVisibleItemPosition();
                pos[1] =Integer.MAX_VALUE;
            } else if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager lm = (LinearLayoutManager) layoutManager;
                pos[0] = lm.findFirstVisibleItemPosition();
                pos[1] = Integer.MAX_VALUE;
            }
        }

        return Math.min(pos[0], pos[1]);
    }
}
